package com.estar.assistsurvy.fragment;

import java.util.ArrayList;
import java.util.List;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.activity.ForgetPwdActivity;
import com.estar.assistsurvy.activity.LoginActivity;
import com.estar.assistsurvy.activity.OrderConfirmActivity;
import com.estar.assistsurvy.adapter.grabAdapter;
import com.estar.assistsurvy.constant.Constants;
import com.estar.assistsurvy.entity.RequestOrder;
import com.estar.assistsurvy.entity.ResponseOrder;
import com.estar.assistsurvy.entity.grabList;
import com.estar.assistsurvy.thread.GrabInfoThread;
import com.estar.assistsurvy.utils.MightypleUtil;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import cn.jpush.android.api.JPushInterface;
import android.widget.ListView;

public class GrabFragment extends BaseFragment implements OnRefreshListener,OnItemClickListener{

	private SwipeRefreshLayout mSwipeLayout;
	private ListView mListView;
	private grabAdapter grba;
	private String deviceId,uuid;
	private ResponseOrder responseOrder;
	private List<grabList> grbaList;
	private ProgressDialog dialog;
	private SharedPreferences userPrefer,rolePrefer;
	private Intent intent;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if(dialog!=null){
        		dialog.dismiss();
        	}
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;
			
			case 0:
				responseOrder=new ResponseOrder();
				responseOrder=(ResponseOrder) msg.obj;
				if(responseOrder.getCode().equals("1")){
					grbaList=new ArrayList<grabList>();
					if(responseOrder.getGrabList()!=null&&responseOrder.getGrabList().size()!=0){
//						for(int i=0;i<responseOrder.getGrabList().length;i++){
//							grbaList.add(responseOrder.getGrabList()[i]);
//						}
						grbaList=responseOrder.getGrabList();
					}else{
						showShortToast("当前已抢单列表为空!");
					}
					grba=new grabAdapter(grbaList, getActivity());
					mListView.setAdapter(grba);
					mSwipeLayout.setRefreshing(false);
				}else if(responseOrder.getCode().equals("0")){
					showShortToast(responseOrder.getMessages());
				}else if(responseOrder.getCode().equals("2")){
					new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_LIGHT)
					.setMessage(responseOrder.getMessages())
					.setNegativeButton("退出", new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							userPrefer.edit().clear().commit();
							rolePrefer.edit().clear().commit();
//							JPushInterface.setAliasAndTags(getActivity().getApplicationContext(), "", null,null);
//							ExitUtils.ExitApp(getActivity());
							JPushInterface.setAliasAndTags(getActivity().getApplicationContext(), "", null,null);
							intent=new Intent(getActivity(), LoginActivity.class);
							startActivity(intent);
							getActivity().finish();
						}
					})
					.setPositiveButton("重新登录", new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							userPrefer.edit().clear().commit();
							rolePrefer.edit().clear().commit();
							JPushInterface.setAliasAndTags(getActivity().getApplicationContext(), "", null,null);
							intent=new Intent(getActivity(), LoginActivity.class);
							startActivity(intent);
							getActivity().finish();
						}
					})
					.create()
					.show();
				}
				break;
				
			default:
				break;
			}

		}

	};
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(view==null){
			view=inflater.inflate(R.layout.fragment_order, null);
		}

		return view;
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		initView();
	}

	@SuppressWarnings("deprecation")
	private void initView(){
		deviceId=MightypleUtil.getDeviceId(getContext());
		rolePrefer=getActivity().getSharedPreferences("role", Context.MODE_PRIVATE);
		userPrefer=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
		uuid=userPrefer.getString("uuid", "");

		mListView = (ListView) view.findViewById(R.id.mListView);
		mListView.setDividerHeight(12);
		mListView.setOnItemClickListener(this);
		mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.mSwipeLayout);

		mSwipeLayout.setOnRefreshListener(this);
		mSwipeLayout.setColorScheme(android.R.color.holo_green_dark, android.R.color.holo_green_light,
				android.R.color.holo_orange_light, android.R.color.holo_red_light);
		//		mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);
		//		mListView.setAdapter(mAdapter);
		mSwipeLayout.setRefreshing(true);
		initList();
	}

	private void initList(){
		dialog=ProgressDialog.show(getActivity(), null, "获取订单列表", false, true);
		RequestOrder requestOrder=new RequestOrder(deviceId, uuid, Constants.ROBBED_ORDER);
		new GrabInfoThread(getActivity(), handler, requestOrder).start();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		initList();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		Intent intent=new Intent(getActivity(), OrderConfirmActivity.class);
		intent.putExtra("orderType", Constants.ROBBED_ORDER);
		intent.putExtra("order", grbaList.get(position));
		startActivity(intent);
	}

}
